package com.ayoldi.podcastusbsync.configurator;

import java.io.File;
import java.util.ArrayList;
import java.util.List;

import com.ayoldi.podcastusbsync.model.Configuration;
import com.ayoldi.podcastusbsync.model.Configuration.GlobalOptions;
import com.ayoldi.podcastusbsync.model.Configuration.UsbDeviceConfiguration;
import com.ayoldi.podcastusbsync.model.Configuration.UsbDeviceConfiguration.PodcastFeed;

public class ConfigurationUtils {
	public static List<PodcastFeed> getFeeds(Configuration configuration){
		List<PodcastFeed> feeds=new ArrayList<PodcastFeed>();
		for (Object obj : configuration.getGlobalOptionsOrUsbDeviceConfiguration()){
			if (obj.getClass().equals(UsbDeviceConfiguration.class)){
				UsbDeviceConfiguration usbDeviceConfig=(UsbDeviceConfiguration)obj;
				feeds.addAll(usbDeviceConfig.getPodcastFeed());
				
			}
		}
		return feeds;
		
		
	}
	
	public static List<UsbDeviceConfiguration> getUsbDeviceConfigurations(Configuration configuration){
		List<UsbDeviceConfiguration> usbDeviceConfigurations=new ArrayList<UsbDeviceConfiguration>();
		for (Object obj : configuration.getGlobalOptionsOrUsbDeviceConfiguration()){
			if (obj.getClass().equals(UsbDeviceConfiguration.class)){
				UsbDeviceConfiguration usbDeviceConfig=(UsbDeviceConfiguration)obj;
				usbDeviceConfigurations.add(usbDeviceConfig);
				
			}
		}
		return usbDeviceConfigurations;
		
		
	}
	
	
	
	public static File getLocalSyncFolder(Configuration configuration){
		for (Object obj : configuration.getGlobalOptionsOrUsbDeviceConfiguration()){
			if (obj.getClass().equals(GlobalOptions.class)){
				GlobalOptions globalOptions=(GlobalOptions)obj;
				File localSyncFolderFile=new File (globalOptions.getLocalSyncFolder());
				return localSyncFolderFile;
				
			}
		}
		return null;
	}
	
	public static GlobalOptions getGlobalOptions(Configuration configuration){
		for (Object obj : configuration.getGlobalOptionsOrUsbDeviceConfiguration()){
			if (obj.getClass().equals(GlobalOptions.class)){
				GlobalOptions globalOptions=(GlobalOptions)obj;
				return globalOptions;
				
			}
		}
		return null;
	}
	
	public static File getAbsoluteFeedDownloadFolder(String localSyncFolder, String feedFolder){
		String absolutePath=localSyncFolder;
		if (!localSyncFolder.endsWith("/")||!localSyncFolder.endsWith("\\")){
			absolutePath=absolutePath+File.separator;
		}
		if (feedFolder.startsWith("/")||feedFolder.startsWith("\\")){
			absolutePath=absolutePath+feedFolder.substring(1, feedFolder.length());
		}
		else{
			absolutePath=absolutePath+localSyncFolder;
		}
		
		File file=new File(absolutePath);
		
		if (!file.exists()){
			file.mkdirs();
		}
		
		return file;
		
	}
	
	public static File getAbsoluteFeedUsbDeviceFolder(File rootDrive, UsbDeviceConfiguration deviceConfiguration, PodcastFeed feed){
		String deviceFolderRelPath=deviceConfiguration.getDeviceFolder();
		File deviceFolder=new File (rootDrive,deviceFolderRelPath);
		File feedFolder=new File (deviceFolder, feed.getFolder());
		
		return feedFolder;
		
	}
	
	public static File getFeedUsbDeviceFolder(File deviceFolder, PodcastFeed feed){
		File feedFolder=new File (deviceFolder, feed.getFolder());
		
		return feedFolder;
		
	}
	
	
}
